package top.buaarobot.webserver.service;

import top.buaarobot.webserver.domain.Comment;
import top.buaarobot.webserver.domain.User;
import top.buaarobot.webserver.repository.CommentRepository;
import top.buaarobot.webserver.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private UserRepository userRepository;

    @Override
    public Iterable<Comment> GetByFatherId(int fatherId) {
        return Adjust(this.commentRepository.searchByFatherId(fatherId));
    }
    @Override
    public Iterable<Comment> GetByCommentId(int commentId) {
        return Adjust(this.commentRepository.searchByCommentId(commentId));
    }
    @Override
    public Iterable<Comment> GetByUserId(int userId) {
        return Adjust(this.commentRepository.searchByUserId(userId));
    }

    @Override
    public String AddComment(Comment comment) {
        this.commentRepository.saveAndFlush(comment);
        // 若为对评论的回复，则该评论计数加一
        if(comment.getCommentId()!=-1){
            Comment fatherComment = commentRepository.findById(comment.getCommentId()).get();
            fatherComment.setResCounts(fatherComment.getResCounts()+1);
            commentRepository.saveAndFlush((fatherComment));
        }
        return "Success"+comment.getId().toString();
    }

    @Override
    public String UpdateComment(int id, String content) {
        Comment comment = this.commentRepository.findById(id).get();
        comment.setContent(content);
        commentRepository.saveAndFlush((comment));
        return "Success";
    }

    @Override
    public String DeleteComment(int id) {
        Comment comment = this.commentRepository.findById(id).get();
        // 若为对评论的回复，则该评论计数减一
        if(comment.getCommentId()!=-1){
            Comment fatherComment = commentRepository.findById(comment.getCommentId()).get();
            fatherComment.setResCounts(fatherComment.getResCounts()-1);
            commentRepository.saveAndFlush((fatherComment));
        }else{  // 若为评论，先删除其下所有回复
            int commentId = comment.getId();
            this.commentRepository.deleteAllByCommentId(commentId);
        }
        commentRepository.deleteById(id);
        return "Success";
    }

    // 添加Owner，并对二级评论更新content
    private Iterable<Comment> Adjust(Iterable<Comment> comments){
        for(Comment comment:comments){
            if(comment.getUserId()==-1) {
                comment.setOwner("匿名用户");
            }else{
                User user = userRepository.findById(comment.getUserId()).get();
                comment.setOwner(user.getNickname());
            }
            if(comment.getCommentId()!=-1){  // 若为二级评论，更新内容
                Comment fatherComment = this.commentRepository.findById(comment.getCommentId()).get();
                comment.setContent(comment.getContent().replaceFirst("<br>.+<br></div>-", "<br>"+fatherComment.getContent()+"<br></div>-"));
                if(fatherComment.getUserId()!=-1){  // 若父评论不是匿名评论，更新回复对象
                    String resUserNickname = this.userRepository.findById(fatherComment.getUserId()).get().getNickname();
                    comment.setContent(comment.getContent().replaceFirst("@.+?&nbsp", "@"+resUserNickname+"&nbsp"));
                }
            }
        }
        return comments;
    }
}
